
import BaseSingleClass from "../base/BaseSingleClass";
import { Event, Node, UIOpacity, tween, v3 } from "cc";
import PoolHelper from "./PoolHelper";
import BaseDialog from "../components/popup/BaseDialog";
//import PanelDialog from "../components/popup/PanelDialog";
import { GameEvent } from "../Const";
export default class PopupHelper extends BaseSingleClass{
    //根节点
    private rootNode:Node=null
    //黑色半透明背景
    private bg:Node=null
    //当前弹窗名称
    private name=''
    private list:Node[]=[]
    init(root:Node,bg:Node):void{
        console.log('PopupHelper.init')
        this.rootNode=root
        this.bg=bg
        this.rootNode.removeAllChildren()
        this.bg.active=false
        //关闭弹窗
        this.rootNode.on(GameEvent.CLOSE_POPUP,this.hide,this)
        //显示帮助
        //this.rootNode.on(GameEvent.SHOW_HELP,this.onShowHelp,this)
        //this.rootNode.on('click',this.onBgClick,true)
        //console.log(this.bg.name,this.rootNode.name)
    }
    onBgClick():void{
        console.log('PopupHelper.onBgClick')
        //
    }
    /*openPanelDialog(name:string,help:string=''):Node{
        const node=this.openDialog('PanelDialog')
        const panel=node.getComponent(PanelDialog)
        panel.helpDialogName=help
        panel.mainDialogName=name
        const pool=PoolHelper.getInstance()
        const dialog=pool.getNode(name)
        dialog.parent=node
        panel.mainDialogNode=dialog
        return dialog
    }*/
    /**
     * 打开弹窗
     * @param name prefab名称
     * @param option 设置回调方法，onShowComplete，onSuccess，onClose，onFail
     * @returns 返回弹窗节点
     */
    openDialog(name:string,
        option?:{onCallBack?:(name:string,data?:any)=>void,onShowComplete?:()=>void,onSuccess?:(data?:any)=>void,onClose?:(data?:any)=>void,onFail?:()=>void},
        showbg=true):Promise<Node>{
        const pool=PoolHelper.getInstance()
        return new Promise<Node>((resolve,reject)=>{
            pool.getNode(name).then(node=>{
                this.show(node,name,showbg,option?.onShowComplete)
                const dialog=node.getComponent(BaseDialog)
                dialog.successCallback=option?.onSuccess
                dialog.closeCallback=option?.onClose
                dialog.failCallback=option?.onFail
                dialog.callBack=option?.onCallBack
                resolve(node)
            }).catch(e=>{
                console.log(e)
                reject(e)
            })
        })
        /*const node=pool.getNode(name).then(node=>{
            this.show(node,'',showbg,option?.onShowComplete)
            const dialog=node.getComponent(BaseDialog)
            dialog.successCallback=option?.onSuccess
            dialog.closeCallback=option?.onClose
            dialog.failCallback=option?.onFail
            dialog.callBack=option?.onCallBack
        })*/
        
        //return node
    }
    /**
     * 显示弹窗
     * @param node 弹窗节点
     * @param name 弹窗名称
     * @param showComplete 弹窗缓动完成回调
     */
    show(node:Node,name='',showbg=true,showComplete?:any):void{
        console.log('PopupHelper.show')
        this.name=name
        //this.rootNode.removeAllChildren()
        this.bg.active=showbg
        this.bg.parent=null
        //this.rootNode.addChild(this.bg)
        //console.log(this.bg.name,this.rootNode.name)
        this.bg.parent=this.rootNode
        //if(this.rootNode.children&&this.rootNode.children.length>0)
        this.bg.setSiblingIndex(this.rootNode.children.length-1)
        //this.rootNode.addChild(node)
        node.parent=this.rootNode
        this.list.push(node)
        //node.parent=this.rootNode
        //node.setScale(0.3,0.3,1)
        const opacity=node.getComponent(UIOpacity)?node.getComponent(UIOpacity):node.addComponent(UIOpacity)
        opacity.opacity=0
        //缓动
        tween(opacity).to(0.4,{opacity:255},{easing:'quadIn'}).call(()=>{
            showComplete&&showComplete()
        }).start()
        //const uit=this.bg.getComponent(UITransform)
        //console.log(scale)
        /*tween(node).to(0.5,{scale:v3(scale,scale,1)},{easing:'quadIn'}).call(()=>{
            showComplete&&showComplete()
        }).start()*/
        //const opacity1=node.getComponent(UIOpacity)
    }
    /**
     * 关闭弹窗,回收节点
     * @param e 事件对象或者节点
     */
    hide(e:Event|Node):void{
        let node:Node,name
        if(e instanceof Node){
            node=e
            name=e.prefab.asset.name
        }else{
            node=e.target as Node
            name=e.target.prefab.asset.name
        }
        
        const pool=PoolHelper.getInstance()
        let temp:Node
        const removeDialog=()=>{
            temp=this.list.pop()
            temp.parent=null
            pool.putNode(temp.prefab.asset.name,temp)
            const dialog=temp.getComponent(BaseDialog)
            /*if(dialog){
                //dialog.onClose()
                dialog.closeCallback&&dialog.closeCallback()
                dialog.closeCallback=()=>{}
            }*/
        }
        removeDialog()
        while(node!=temp&&this.list.length>0){
            removeDialog()
        }

        if(this.list.length>0){
            const last=this.list[this.list.length-1]
            this.bg.setSiblingIndex(this.rootNode.children.length-1)
            last.setSiblingIndex(this.rootNode.children.length-1)
        }else{
            this.rootNode.removeAllChildren()
        }
    }
    onShowHelp():void{
        //
    }
}